package com.example.demo.controller;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
@Controller
//@ResponseBody
@RequestMapping("/test")
// @RestController
public class TestController {

    /**
     * 请求转发 forward
     * @return
     */
    @RequestMapping("/hi")
    public String hi() {
        return "/hello.html";
//        return "forward:/hello.html";
    }

    /**
     * 请求转发 forward
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/hi2")
    public void hi2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // req的获取请求调度器
        // forward需要传递 req 和 resp 参数
        req.getRequestDispatcher("/hello.html").forward(req, resp);
    }

    /**
     * 请求重定向 redirect
     * @return
     */
    @RequestMapping("/hello")
    public String hello() {
        return "redirect:/hi.html";
    }

    @RequestMapping("/hello2")
    public void hello2(HttpServletResponse resp) throws IOException {
        resp.sendRedirect("/hi.html");
    }

    @RequestMapping("/html")
    public String getHtml() {
        return "<h1>这是一条text/html格式的数据</h1>";
    }

    @RequestMapping("/json")
    public Map<String, String> getJson() {
        Map<String, String> map = new HashMap<>();
        map.put("zhangsan", "123");
        map.put("lisi", "456");
        map.put("wangwu", "789");
        return map;
    }
}
